<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Data.OleDb" %>

<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Sorting and Finding Records in a DataView</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Sorting and Finding Records in a DataView</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server"/>

<div>Connection string: <b><span id="outConnect" runat="server"></span></b></div>
<div>SELECT command: <b><span id="outSelect" runat="server"></span></b></div>
<div id="outError" runat="server"></div><br />

<div id="outMessage" runat="server"></div>

<form runat="server">
Sort records: <input type="submit" id="cmdTitle" value="By Title" runat="server" /> &nbsp;
<input type="submit" id="cmdISBN" value="By ISBN" runat="server" /> &nbsp;
<input type="submit" id="cmdDate" value="By Date" runat="server" /><p />
Search within titles: <input type="text" id="txtFind" size="20" runat="server" />
<input type="submit" id="cmdFind" value="Find" runat="server" />
</form>

<asp:datagrid id="dgrResult" runat="server" /><br />

<script language="vb" runat="server">

Sub Page_Load()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect = ctlConnectStrings.OLEDBConnectionString
   outConnect.innerText = strConnect 'and display it

   'specify the SELECT statement to extract the data
   Dim strSelect As String
   strSelect = "SELECT * FROM BookList WHERE ISBN LIKE '1861003%'"
   outSelect.innerText = strSelect   'and display it

   'create a new DataSet object
   'note that we have to create it outside the Try..Catch block
   'as this is a separate block and so is a different scope
   Dim objDataSet As New DataSet()

   Try

      'create a new Connection object using the connection string
      Dim objConnect As New OleDbConnection(strConnect)

      'create a new DataAdapter using the connection object and select statement
      Dim objDataAdapter As New OleDbDataAdapter(strSelect, objConnect)

      'fill the dataset with data from the DataAdapter object
      objDataAdapter.Fill(objDataSet, "Books")

   Catch objError As Exception

      'display error details
      outError.innerHTML = "<b>* Error while accessing data</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution

   End Try

   'create a DataView object for the Books table in the DataSet
   Dim objDataView As New DataView(objDataSet.Tables("Books"))

   'sort the records into the correct order
   If Len(Request.Form("cmdTitle")) > 0 Then
      objDataView.Sort = "Title"
      outMessage.innerHTML = "DataView.Sort = <b>" & objDataView.Sort & "</b>"
   End If
   If Len(Request.Form("cmdISBN")) > 0 Then
      objDataView.Sort = "ISBN"
      outMessage.innerHTML = "DataView.Sort = <b>" & objDataView.Sort & "</b>"
   End If
   If Len(Request.Form("cmdDate")) > 0 Then
      objDataView.Sort = "PublicationDate DESC, Title"
      outMessage.innerHTML = "DataView.Sort = <b>" & objDataView.Sort & "</b>"
   End If

   'or find matching records
   If Len(Request.Form("cmdFind")) > 0 Then
      objDataView.RowFilter = "Title LIKE '*" & txtFind.value & "*'"
      outMessage.innerHTML = "DataView.RowFilter = <b>" & objDataView.RowFilter & "</b>"
   End If

   'assign the DataView object to the DataGrid control
   dgrResult.DataSource = objDataView
   dgrResult.DataBind()  'and bind (display) the data

End Sub
</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
